package cn.hd.shop_springboot.controller;

import cn.hd.shop_springboot.pojo.User;
import cn.hd.shop_springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebParam;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @RequestMapping("/userlist")
    public String userlist(Model model){
        List<User> list =  userService.list();
        model.addAttribute("userlist",list);
        return "User/userlist";
    }

    @GetMapping("/deleteuser")
    public String deleteUser(@RequestParam("userid") int userid){
        userService.delete(userid);
        return "redirect:userlist";
    }

    @GetMapping("/toadduser")
    public String toadduser(){
        return "User/adduser";
    }

    @RequestMapping("/adduser")
    public String insertUser(User user){
        userService.add(user);
        return "redirect:userlist";
    }

    @RequestMapping("/toupdate")
    public String updateUser(@RequestParam("userid") int userid, Model model){
        User user = userService.get(userid);
        model.addAttribute("user",user);
        return "User/updateuser";
    }

    @RequestMapping("/updateuser")
    public String updateUser(User user){
        userService.update(user);
        return "redirect:userlist";
    }

}
